/* SharePoint Console Project
 * Http://SharePointConsole.codeplex.com
 * Copyright Jean-Alain Baeyens (2009)
 * Software under Microsoft Reciprocal License (Ms-RL)
 */

using System;
using System.Xml;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Sharepoint.Console.Commands;

namespace Sharepoint.Console.List
{
    public class ChangeDefaultView : RecursiveBaseCommande
    {
        public override string ShortHelp
        {
            get
            {
                return "Change the default view of a list";
            }
        }

        public override string LongHelp
        {
            get
            {
                return "Change the default view of a list given by this url. The new default view is identified by this name and must exist.";
            }
        }

        public override string Syntax
        {
            get
            {
                return "Syntax: ChangeDefaultView 'ListUrl' 'DefaultView'";
            }
        }

        public override string Execute(string[] args)
        {
            if (args.Length != 3) return string.Format("Invalid number of parameters. {0}",Syntax);

            string listUrl = string.Format("{0}/{1}",this.SharepointConnection.Web.Url, args[1]);
            string defaultView = args[2];

            SPList list = this.SharepointConnection.Web.GetList(listUrl);
            if (list != null)
            {
                try
                {
                    SPView view = list.Views[defaultView];
                    view.DefaultView = true;
                    view.Update();
                    return "Default view updated";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
            else
            {
                return "Invalid list";
            }
        }
    }
}

